<?php

if (!class_exists("PST_SELECT")) {

    class PST_SELECT extends PST_PRESENTATION {

        protected $array = array("type" => "select");
        protected $model = null;
        protected $plugin = null;
        protected $model_obj = null;
        protected $conditions = array();
        protected $order = array();

        public function __construct($settings) {
            parent::__construct($settings);
            if (isset($this->classparams['model'])) {
                $this->model = $this->classparams['model'];
            }
            if (isset($this->classparams['order'])) {
                $this->order = $this->classparams['order'];
            }
            $this->model_obj = $this->getObj();
            if (isset($this->model_obj)) {
                $this->array["options"] = $this->getSQLArray();

                if (!empty($this->initialvalue) && empty($this->array["options"][$this->initialvalue])) {
                    $this->array["options"][$this->initialvalue] = $this->initialvalue;
                }
            }
        }

        public function renderTableMaint() {
            if (isset($this->isvisible) && !$this->isvisible) {
                return '<input type="hidden" name="' . $this->name . '" id="' . $this->id . '" value="' . $this->value . '" />';
            }

            if ($this->readonly) {
                $this->html = false;
                return $this->renderReadOnly();
            }

            $html = '<div id="box' . $this->id . '" class="input select ' . $this->required . '">';
            $html.= '<label for="' . $this->id . '">' . $this->label . '</label>';
            if (isset($this->array["options"])) {
                $html.= '<select name="' . $this->name . '" id="' . $this->id . '">';
                $html.= '<option value=""></option>';
                foreach ($this->array["options"] as $key => $value) {
                    $selected = ($key == $this->value ? 'selected="selected"' : '');
                    $html.= '<option value="' . $key . '" ' . $selected . '>' . $value . '</option>';
                }
                if (!empty($this->initialvalue) && !in_array($this->initialvalue, keys($this->array['options']))) {
                    $html.= '<option value="' . $this->initialvalue . '" selected="selected">' . $this->initialvalue . '</option>';
                }
                $html.= '</select>';
            }
            if (!empty($this->note)) {
                $html.= '<div class="note">' . $this->note . '</div>';
            }
            $html.= '</div>';
            return $html;
        }

        protected function getHelperValue() {
            if (isset($this->model_obj)) {
                $sql = array(
                    'fields' => array($this->model_obj->primaryKey, $this->model_obj->displayField),
                    'conditions' => array($this->model_obj->primaryKey => $this->value),
                );

                $this->array["options"] = $this->model_obj->find('list', $sql);
            }

            if (isset($this->array["options"][$this->value])) {
                return $this->array["options"][$this->value];
            }

            return $this->value;
        }

        protected function getObj() {
            if (isset($this->model) && $url = $this->existeModelo()) {
                include_once($url);
                if (class_exists($this->model)) {
                    return new $this->model;
                }
            }
            return null;
        }

        private function getSQLArray() {
            $sql = array(
                'fields' => array($this->model_obj->primaryKey, $this->model_obj->displayField),
                'conditions' => $this->conditions,
                'order' => $this->order
            );

            return $this->model_obj->find('list', $sql);
        }

        protected function existeModelo() {
            if (file_exists(ROOT . '/app/Model/' . ucfirst($this->model) . '.php')) {
                return ROOT . '/app/Model/' . ucfirst($this->model) . '.php';
            } else {
                foreach (scandir(ROOT . '/app/Plugin/') as $dir) {
                    if (strpos($dir, '.') === false && is_dir(ROOT . '/app/Plugin/' . $dir)) {
                        if (file_exists(ROOT . '/app/Plugin/' . $dir . '/Model/' . ucfirst($this->model) . '.php')) {
                            return ROOT . '/app/Plugin/' . $dir . '/Model/' . ucfirst($this->model) . '.php';
                        }
                    }
                }
            }
            return false;
        }

    }

}